<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <input type="button" value="新建" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal"/>
                                        <button type="button" onclick="deleteBatch()" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
							<form id="typeForm">
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input type="text" name="name"  >
									<input type="button" onclick="getTypeList()" value="查询" class="btn btn-default"/>
                                </div>
                            </div></form>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="typeContent">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->



  <!-- 增加窗口 -->
  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myAddModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myAddModalLabel">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <td><input id="addName"  class="form-control" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brands" style="width: 100%" multiple>
								  <option th:each="brand : ${brandList}" th:value="${brand.id}">
									  [[${brand.name}]]
								  </option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="specOptions" style="width: 100%" multiple>
								  <option th:each="spec : ${specList}" th:value="${spec.id}">
									  [[${spec.specName}]]
								  </option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="addAttributeContent">
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <input type="button" onclick="add()" value="保存" class="btn btn-success" data-dismiss="modal" aria-hidden="true"/>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
<!-- 编辑窗口 -->
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <td id="updateNames"></td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="updateBrands" style="width: 100%" multiple>
								  <option th:each="brand : ${brandList}" th:value="${brand.id}">
									  [[${brand.name}]]
								  </option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="updateSpecOptions" style="width: 100%" multiple>
								  <option th:each="spec : ${specList}" th:value="${spec.id}">
									  [[${spec.specName}]]
								  </option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="updateAttributeContent">
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <input type="button" onclick="update()" value="保存" class="btn btn-success" data-dismiss="modal" aria-hidden="true"/>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

<script>

	$(function(){
		getTypeList();
		$("#brands").select2()
		$("#specOptions").select2()
	})

	function getTypeList(){
		$.ajax({
			url:"/typeController/getTypeList",
			data:$("#typeForm").serialize(),
			dataType:"json",
			type:"post",
			async:false,
			success:function (result){
				var list = result.result
				var dd = ''

				for (i in list){
					dd += '<tr>'
					dd += '<td><input name="type" value="' + list[i].id + '"  type="checkbox"></td>'
					dd += '<td>' + list[i].id + '</td>'
					dd += '<td>' + list[i].name + '</td>'

					var brandIds = ''
					var brandIdsStr = list[i].brandIds
					var brandIdsJson = JSON.parse(brandIdsStr)
					for(j in brandIdsJson){
						brandIds += "," + brandIdsJson[j].text
					}
					brandIds = brandIds.substring(1, brandIds.length)

					dd += '<td>' + brandIds + '</td>'

					var specIds = ''
					var specIdsStr = list[i].specIds
					var specIdsJson = JSON.parse(specIdsStr)
					for(j in specIdsJson){
						specIds += "," + specIdsJson[j].text
					}
					specIds = specIds.substring(1, specIds.length)

					dd += '<td>' + specIds + '</td>'

					var customAttributeItems = ''
					var customAttributeItemsStr = list[i].customAttributeItems
					var customAttributeItemsJson = JSON.parse(customAttributeItemsStr)
					for(j in customAttributeItemsJson){
						customAttributeItems += "," + customAttributeItemsJson[j].text
					}
					customAttributeItems = customAttributeItems.substring(1, customAttributeItems.length)

					dd += '<td>' + customAttributeItems + '</td>'
					dd += '<td class="text-center">'
					dd += '<input type="button" value="修改" onclick="toUpdate(' + list[i].id + ')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" />'
					dd += '</td>'
					dd += '</tr>'
				}

				$("#typeContent").html(dd)
			},
			error:function (){
				alert("系统错误")
			}
		})
	}

	function add(){
		var name = $("#addName").val()
		console.log(name)

		var brandSelect = $("#brands").select2("data")
		var brandJSONList = []
		for(i in brandSelect) {
			var brandJSON = {id: brandSelect[i].id, text: brandSelect[i].text.trim()}
			brandJSONList.push(brandJSON)
		}
		var brandListStr = JSON.stringify(brandJSONList)
		console.log(brandListStr)

		var specOptionSelect = $("#specOptions").select2("data")
		var specOptionJSONList = []
		for(i in specOptionSelect) {
			var specOptionJSON = {id: specOptionSelect[i].id, text: specOptionSelect[i].text.trim()}
			specOptionJSONList.push(specOptionJSON)
		}
		var specOptionListStr = JSON.stringify(specOptionJSONList)
		console.log(specOptionListStr)

		var trs = $("#addAttributeContent").find("tr")
		var attributeList = []
		for (var i = 0; i < trs.length; i++){
			var tds = $(trs[i]).find("td")
			var attribute = {text:$(tds[1]).find("input").val()}
			attributeList.push(attribute)
		}
		var attributeListStr = JSON.stringify(attributeList)
		console.log(attributeListStr)

		$.ajax({
			url:"/typeController/addType",
			data:{"name":name, "brandIds":brandListStr, "specIds":specOptionListStr, "customAttributeItems":attributeListStr},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result){
				var msg = result.message;
				window.location.reload()
				alert(msg)
			},
			error:function (){
				alert("系统错误!")
			}
		})
	}

	function toUpdate(id){
		$("#updateBrands").select2()
		$("#updateSpecOptions").select2()

		$.ajax({
			url:"/typeController/toUpdate/" + id,
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				var type = result.result
				var dd = "<input id='updateId' type='hidden' value='" + type.id + "'>" +
						"<input id='updateName' value='" + type.name + "' class=\"form-control\">  "
				$("#updateNames").html(dd)

				var brandJSONStr = type.brandIds
				var brandJSON = JSON.parse(brandJSONStr)
				var brandIdsArr = []
				for(var i = 0; i < brandJSON.length; i++){
					brandIdsArr.push(brandJSON[i].id)
				}
				$("#updateBrands").val(brandIdsArr).trigger("change")

				var specOptionJSONStr = type.specIds
				var specOptionJSON = JSON.parse(specOptionJSONStr)
				var specOptionIdsArr = []
				for(var i = 0; i < specOptionJSON.length; i++){
					specOptionIdsArr.push(specOptionJSON[i].id)
				}
				$("#updateSpecOptions").val(specOptionIdsArr).trigger("change")

				var attributeJSONStr = type.customAttributeItems
				var attributeJSON = JSON.parse(attributeJSONStr)
				dd = ''
				for(var i = 0; i < attributeJSON.length; i++){
					dd += '<tr>' +
							'<td><input name="updateCheckbox" id="' + i + '" type="checkbox" class="icheckbox_square-blue" ></td>' +
							'<td><input name="customAttributeItems" value="' + attributeJSON[i].text + '" class="form-control" placeholder="属性名称" ></td>' +
							'<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>' +
							'</tr>'
				}
				$("#updateAttributeContent").html(dd)
			}
		})
	}

	function update(){
		var id = $("#updateId").val()

		var name = $("#updateName").val()

		var brandSelect = $("#updateBrands").select2("data")
		var brandJSONList = []
		for(i in brandSelect) {
			var brandJSON = {id: brandSelect[i].id, text: brandSelect[i].text.trim()}
			brandJSONList.push(brandJSON)
		}
		var brandListStr = JSON.stringify(brandJSONList)
		console.log(brandListStr)

		var specOptionSelect = $("#updateSpecOptions").select2("data")
		var specOptionJSONList = []
		for(i in specOptionSelect) {
			var specOptionJSON = {id: specOptionSelect[i].id, text: specOptionSelect[i].text.trim()}
			specOptionJSONList.push(specOptionJSON)
		}
		var specOptionListStr = JSON.stringify(specOptionJSONList)
		console.log(specOptionListStr)

		var trs = $("#updateAttributeContent").find("tr")
		var attributeList = []
		for (var i = 0; i < trs.length; i++){
			var tds = $(trs[i]).find("td")
			var attribute = {text:$(tds[1]).find("input").val()}
			attributeList.push(attribute)
		}
		var attributeListStr = JSON.stringify(attributeList)
		console.log(attributeListStr)

		$.ajax({
			url:"/typeController/updateType",
			data:{"id":id, "name":name, "brandIds":brandListStr, "specIds":specOptionListStr, "customAttributeItems":attributeListStr},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result){
				var msg = result.message;
				window.location.reload()
				alert(msg)
			},
			error:function (){
				alert("系统错误!")
			}
		})
	}

	function deleteBatch(){
		var allTypes = document.getElementsByName("type")
		console.log(allTypes)
		var checked = []
		for(i in allTypes){
			if(allTypes[i].checked){
				checked.push(allTypes[i].value)
			}
		}

		if(checked.length <= 0){
			alert("请选择模板!");
			return
		}

		console.log(checked)

		$.ajax({
			url:"/typeController/deleteBatch",
			traditional: true,
			data:{"ids" : checked},
			dataType:"json",
			type:"get",
			async:false,
			success:function (result){
				var msg = result.message
				window.location.reload()
				alert(msg)
			},
			error:function (){
				alert("系统错误!")
			}
		})
	}

</script>
</body>
</html>